//
//  TBViewController.swift
//  TestDemo
//
//  Created by ayin on 2019/2/23.
//  Copyright © 2019 aYin. All rights reserved.
//

import UIKit

public let isIphoneX: Bool                                     = UIScreen.main.bounds.size.height == 812
public let NavigationH: CGFloat                                = isIphoneX ? 88 : 64
public let TabBarH: CGFloat                                    = isIphoneX ? 83 : 49

class TBViewController: UIViewController,UIScrollViewDelegate {
    //头部筛选条
    lazy var navView: UIScrollView = {
        let scrollView = UIScrollView(frame: .zero)
        scrollView.backgroundColor = UIColor.white
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    
    lazy var topFilterView: CalendarTopFilterView = {
        let view = CalendarTopFilterView(frame: .zero)
        return view
    }()
    
    lazy var contentScrollView: UIScrollView = {
        let scrolLView = UIScrollView(frame: CGRect.zero)
        scrolLView.backgroundColor = UIColor.white
        scrolLView.delegate = self
//        scrolLView.bounces = false
        scrolLView.isPagingEnabled = true
        return scrolLView
    }()

    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        return tableView
    }()
    
    var scale:CGFloat = 1
    var currentOffsetY:CGFloat = 0.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNav()
        setupUI()
    }
    
    //MARK: - 搭建UI
    func setupNav() {
        self.view.addSubview(navView)
        navView.addSubview(topFilterView)
        
        navView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(NavigationH)
        }
        
        topFilterView.backgroundColor = UIColor.magenta
        topFilterView.snp.makeConstraints { (make) in
            make.left.equalTo(navView.snp.left)
            make.width.equalTo(screenWidth - 60)
            make.bottom.equalTo(NavigationH)
            make.height.equalTo(54.up)
        }
        topFilterView.refreshDatas([CTopFilterModel(title: "线上是是", state: "0"),CTopFilterModel(title: "海外", state: "0"),CTopFilterModel(title: "中国", state: "0"),CTopFilterModel(title: "中国", state: "0"),CTopFilterModel(title: "中国", state: "0"),CTopFilterModel(title: "中国", state: "0")])
        
//        topFilterView.layer.anchorPoint = CGPoint(x: 0, y: 34)
    }
    
    func setupUI() {
        self.view.backgroundColor = UIColor.white
        contentScrollView.backgroundColor = UIColor.orange;
        contentScrollView.contentSize = CGSize(width: screenWidth*3, height: screenHeight - NavigationH)
        self.view.addSubview(contentScrollView)
        contentScrollView.snp.makeConstraints { (make) in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
//            make.height.equalTo(screenHeight - NavigationH)
        }
        
        for i in 0 ..< 3 {
            let vie = UIView()
            contentScrollView.addSubview(vie)

            let tableView = UITableView(frame: .zero, style: .plain)
            tableView.showsVerticalScrollIndicator = false
            tableView.separatorStyle = .none
            tableView.backgroundColor = UIColor.white
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
            tableView.backgroundColor = [UIColor.red,UIColor.green,UIColor.yellow][i]
            vie.addSubview(tableView);

            vie.snp.makeConstraints { (make) in
                make.top.equalToSuperview()
                make.left.equalTo(screenWidth*CGFloat(i))
                make.width.equalTo(screenWidth)
                make.height.equalTo(screenHeight - NavigationH)
//                make.bottom.equalToSuperview()
            }
            
            tableView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
        }
    }
}

extension TBViewController {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.x != 0 {
            if scrollView is UITableView {
                scrollView.isScrollEnabled = false
            }
            contentScrollView.isScrollEnabled = true
        } else if scrollView.contentOffset.y != 0 {
            if scrollView is UITableView {
                scrollView.isScrollEnabled = true
                contentScrollView.isScrollEnabled = false
                let offsetY = scrollView.contentOffset.y
                print("-------------off\(offsetY)")
                var top:CGFloat = 0
                if offsetY >= 0 { //上
                    top = offsetY > 20 ? -20 : -offsetY
                }else if offsetY < 0 {//下
                    top = abs(offsetY) > 0 ? 0 : abs(offsetY)
                }
                
                navView.snp.remakeConstraints { (make) in
                    make.left.right.equalToSuperview()
                    make.height.equalTo(NavigationH)
                    make.top.equalTo(top)
                }
                
                if offsetY > 0 {
                    scale = scale - 0.01
                }else {
                    scale = scale + 0.01
                }
                
                var off:CGFloat = 0;
                if offsetY >= 0 {
                    off = offsetY > 20 ? 20 : offsetY;
                } else {
                    off = offsetY < 0 ? 0 : offsetY;
                }
//                if self.currentOffsetY < offsetY {
//                    self.topFilterView.refreshSelectItemFontWithVertical(off)
//                } else {
//                    self.topFilterView.refreshSelectItemFontWithVertical(-off)
//                }
                self.currentOffsetY = offsetY;
            }
            contentScrollView.isScrollEnabled = false
        }
    }
}

extension TBViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        cell.textLabel?.text = "title\(indexPath.row)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.navigationController?.pushViewController(ViewController(), animated: true)
    }
}

class TBHeaderView:UIView {
    
}

class TBCell:UITableViewCell {
    
}
